<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\Ip;
use App\Transformers\IpTransformer;

class IpsController extends Controller
{
    public function index()
    {
    	return $this->response->collection(Ip::all(), new IpTransformer());
    }


    public function show(Ip $ip)
    {
    	return $this->response->item($ip, new IpTransformer());
    }

    /** [store 新增] */
    public function store(Request $request, Ip $ip)
    {
    	$ip->fill($request->all());
    	$ip->save();

    	return $this->response->item($ip, new IpTransformer())->setStatusCode(201);
    }

    public function update(Request $request, Ip $ip)
    {
    	$ip->fill($request->all());
    	$ip->update();

    	return $this->response->item($ip, new IpTransformer());
    }

    public function destroy(Ip $ip)
    {
    	$ip->delete();
        return $this->response->noContent();
    }

    /** [setInformation 设置 IP 来源信息] */
    public function setInformation(Request $request, Ip $ip)
    {
        // 清除中间表数据 
       $ip->informations()->detach();

        if ($request->information_ids !== null ){
    	 	$ip->add_information(explode(',', $request->information_ids));
        }

	 	return $this->response->item($ip, new IpTransformer());
    }



}
